﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.ObjectModel;
using System.Threading;


using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace CStock
{
    public class StockReader
    {

       
        public static int UpdateIntervalInSeconds = 12;

        public List<string> stockSyms = new List<string>();
        private bool inThread = false;
        private bool inetConnection = false;
        public IFinancialData FinancialData;
        public StockReader()
        {
            

            inetConnection = CheckInetConnection();

           
            if (inetConnection)
            {
                Thread updateThread = new Thread(new ThreadStart(UpdateStockQuotes));
                updateThread.Start();
            }
        }

        private bool CheckInetConnection()
        { 
            try
            {
                System.Net.Sockets.TcpClient clnt=new System.Net.Sockets.TcpClient("www.google.com",80);
                clnt.Close();
                return true;
            }
            catch(Exception)
            {
                return false;
            }
        }

        public void UpdateStockQuotes()
        {
            FinancialData.UpdateFromWeb(this.inThread);
        }

    
        public void Timer_Tick(object sender, EventArgs eArgs)
        {
            inetConnection = CheckInetConnection();

            if (inetConnection)
            {
                Thread updateThread = new Thread(new ThreadStart(UpdateStockQuotes));
                updateThread.Start();
            }

           
        }

        public bool InetConnection
        {
            get{ return inetConnection; }
            set { inetConnection = value;}
        }
    }

    
}
